package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.PurchaseDao;
import cn.gson.crm.model.dao.PurchasexqDao;
import cn.gson.crm.model.dao.SupplierDao;
import cn.gson.crm.model.mapper.CaigouMapper;
import cn.gson.crm.model.pojos.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.Position;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class Cpcgservice {
    @Autowired
    CaigouMapper caigouMapper;
    @Autowired
    PurchaseDao purchaseDao;
    @Autowired
    PurchasexqDao purchasexqDao;
    @Autowired
    SupplierDao supplierDao;

    //分页查询采购单
    public List<Purchasexq> cpcg(String name,String datas,String dates1){
        System.err.println("开始时间:"+datas);
        System.err.println("结束时间:"+dates1);
        return caigouMapper.cxcg(name,datas,dates1);
    }

    //采购新增
    public void cg(String positionList){
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+positionList);
        JSONObject jsonObject=JSONObject.parseObject(positionList);
        System.out.println(jsonObject);
        String purchasexq=String.valueOf(jsonObject.get("purchasexq").toString());
        System.out.println(purchasexq);
        String supplier=String.valueOf(jsonObject.get("supplier").toString());
        System.out.println(supplier);
        String purchase=String.valueOf(jsonObject.get("purchase").toString());
        System.out.println(purchase);

            if(purchase.substring(1,2).equals("0")) {
                //采购主表新增
                String str2 = purchase;
                JSONArray jsonArray = JSONArray.parseArray(str2);
                Purchase purchase1 = new Purchase();
                purchase1.setPurchaseJbr((String) jsonArray.get(1));
                purchase1.setPurchaseDh((String) jsonArray.get(2));
                purchase1.setPurchasexqSfrk((String) jsonArray.get(3));
                purchase1.setPurchasePrice((Integer) jsonArray.get(4));
                Date time;
                Date time1;
                SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    time = defaultFormat.parse((String) jsonArray.get(6));
                    time1 = defaultFormat.parse((String) jsonArray.get(5));
                    purchase1.setPurchaseData(time);
                    purchase1.setNeedData(time1);
                } catch (Exception e) {
                    e.fillInStackTrace();
                }
                purchaseDao.save(purchase1);
                //查询供应商表
                String str1 = supplier;
                JSONObject jsonObject1 = JSONObject.parseObject(str1);
                String supplierName = String.valueOf(jsonObject1.get("supplierName").toString());
                Integer supplierId = Integer.parseInt(jsonObject1.get("supplierId").toString());
                String supplierLxr = String.valueOf(jsonObject1.get("supplierLxr").toString());
                String supplierPhone = String.valueOf(jsonObject1.get("supplierPhone").toString());

                //采购从表新增
                String str = purchasexq;
                JSONArray array = JSONArray.parseArray(str);
                List<String> list = JSONObject.parseArray(array.toJSONString(), String.class);
                System.out.println(list);
                for (String aa : list) {
                    System.out.println(aa);
                    JSONArray jsonArray2 = JSONArray.parseArray(aa);
                    for (int j = 0; j < jsonArray2.size() / 9; j++) {
                        System.out.println(jsonArray2.get(j));
                        Purchasexq purchasexq1 = new Purchasexq();
                        Supplier ji = new Supplier();
                        Pro id = new Pro();
                        purchasexq1.setPurchasexqDw((String) jsonArray2.get(0));
                        purchasexq1.setPurchasexqCc((String) jsonArray2.get(1));
                        purchasexq1.setPurchasexqYs((String) jsonArray2.get(2));
                        int a = (Integer) jsonArray2.get(3);
                        long b = (int)a;
                        purchasexq1.setPurchasexqNumber(b);
                        int c = (Integer) jsonArray2.get(4);
                        long d = (int)a;
                        purchasexq1.setPurchasexqDj(d);
                        purchasexq1.setPurchaseName((String) jsonArray2.get(5));
                        purchasexq1.setPurchasexqBz((String) jsonArray2.get(7));
                        id.setCpId((Integer) jsonArray2.get(8));
                        purchasexq1.setPro10(id);
                        purchasexq1.setPurchase(purchase1);
                        ji.setSupplierId(supplierId);
                        purchasexq1.setSupplier(ji);
                        purchasexqDao.save(purchasexq1);
                    }
                }
        }else {//hhh
                String str2 = purchase;
                JSONArray jsonArray = JSONArray.parseArray(str2);
                Purchase purchase1 = new Purchase();
                purchase1.setPurchaseId((Integer) jsonArray.get(0));
                purchase1.setPurchaseJbr((String) jsonArray.get(1));
                purchase1.setPurchaseDh((String) jsonArray.get(2));
                purchase1.setPurchasexqSfrk((String) jsonArray.get(3));
                purchase1.setPurchasePrice((Integer) jsonArray.get(4));
                Date time2;
                Date time3;
                SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    time2 = defaultFormat.parse((String) jsonArray.get(6));
                    time3 = defaultFormat.parse((String) jsonArray.get(5));
                    purchase1.setPurchaseData(time2);
                    purchase1.setNeedData(time3);
                } catch (Exception e) {
                    e.fillInStackTrace();
                }
                purchaseDao.save(purchase1);
                //查询供应商表
                String str1=supplier;
                JSONObject jsonObject1=JSONObject.parseObject(str1);
                String supplierName=String.valueOf(jsonObject1.get("supplierName").toString());
                Integer supplierId=Integer.parseInt(jsonObject1.get("supplierId").toString());
                String supplierLxr=String.valueOf(jsonObject1.get("supplierLxr").toString());
                String supplierPhone=String.valueOf(jsonObject1.get("supplierPhone").toString());

                //采购从表新增
                String str=purchasexq;
                JSONArray array=JSONArray.parseArray(str);
                List<String> list=JSONObject.parseArray(array.toJSONString(),String.class);
                System.out.println(list);
                for (String aa:list) {
                    System.out.println(aa);
                    JSONArray jsonArray2=JSONArray.parseArray(aa);
                    for (int j=0;j<jsonArray2.size()/9;j++){
                        System.out.println(jsonArray2.get(j));
                        Purchasexq purchasexq1=new Purchasexq();
                        Supplier ji=new Supplier();
                        Pro id = new Pro();
                        purchasexq1.setPurchasexqDw((String) jsonArray2.get(0));
                        purchasexq1.setPurchasexqCc((String) jsonArray2.get(1));
                        purchasexq1.setPurchasexqYs((String) jsonArray2.get(2));
                        purchasexq1.setPurchasexqNumber((Long) jsonArray2.get(3));
                        purchasexq1.setPurchasexqDj((Long) jsonArray2.get(4));
                        purchasexq1.setPurchaseName((String) jsonArray2.get(5));
                        purchasexq1.setPurchasexqId((Integer) jsonArray2.get(6));
                        purchasexq1.setPurchasexqBz((String) jsonArray2.get(7));
                        id.setCpId((Integer) jsonArray2.get(8));
                        purchasexq1.setPurchase(purchase1);
                        ji.setSupplierId(supplierId);
                        purchasexq1.setSupplier(ji);
                        purchasexq1.setPro10(id);
                        purchasexqDao.save(purchasexq1);
                    }
                }
            }
    }





    //查询采购名称
    public List<Pro> cgmc(){
        return caigouMapper.cgmc();
    }

    //查询供货商下拉框
    public List<Supplier> ghs(){
        return caigouMapper.ghs();
    }

    //根据供货商名查询联系电话
    public List<Supplier> gh(String ghsm){
       return caigouMapper.ghsm(ghsm);
    }

    //根据供货商联系人查询联系电话
    public List<Supplier> dian(String xi){
        return caigouMapper.dian(xi);
    }

    //添加产品
    public List<SpecifValue> tjcp(){
        return caigouMapper.tjcp();
    };

    //根据产品名称查询
    public List<ProductXq> cpmc(){
        return caigouMapper.mccx();
    }

    //根据采购id查询采购明细表
    public List<Purchasexq> cgid(Integer cgid){
        return caigouMapper.cgid(cgid);
    }

    //根据产品名称查询产品id
    public List<Pro> chanpinid(String chanpinid){
        return caigouMapper.chanpinid(chanpinid);
    }

    //查询采购明细
    public List<Purchasexq> cgmx1(Integer caigouid){
        return caigouMapper.cgmx1(caigouid);
    }

    //查询经办人
    public List<User> user(){
        return caigouMapper.user();
    }
}
